package com.bjsdzk.collector.timer;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.bjsdzk.collector.serviceImp.TelecomSecondService;
import com.bjsdzk.collector.serviceImp.TelecomService;

@Component
//@Profile("pro")
//@Profile("dev")
@Profile("test")
public class TelecomTask {

	private static Logger logger = LoggerFactory.getLogger(TelecomTask.class);
    @Autowired
    private TelecomService telecomService;
    @Autowired
    private TelecomSecondService telecomSecondService;
//	@Resource(name="secondMongoTemplate")
//    private MongoTemplate secondMongoTemplate;
//	@Autowired
//    private MongoTemplate mongoTemplate;
	    
	@PostConstruct
	public void init() {
//		try {
//			telecomService.getToken();
//			logger.debug("电信平台获取token");
//		} catch (Exception e) {
//			e.printStackTrace();
//			logger.error("电信平台telecomService获取token出错");
//		}
		try {
			telecomSecondService.getToken();
			logger.debug("电信平台获取token");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("电信平台telecomSecondService获取token出错");
		}
	}
	
	//@Scheduled(cron = "0 0/1 * * * ?")
	@Scheduled(cron = "0 25 * * * ?")
	public void accessToken() {
//		try {
//    		telecomService.refreshToken();
//    		logger.debug("电信平台xiyu定时器刷新token");
//		} catch (Exception e) {
//			logger.error("电信平台telecomService获取token出错");
//			e.printStackTrace();
//		}
		try {
				telecomSecondService.refreshToken();
	    		logger.debug("ours电信平台定时器刷新token");
			} catch (Exception e) {
				logger.error("电信平台telecomSecondService获取token出错");
				e.printStackTrace();
			}
	}
/*		
	//@Scheduled(fixedDelay=60*1000)
	public void mongo() {
		logger.debug("定时器获取远大mongo数据");
		try {
//			moveData(Company.class);
//			moveData(Device.class );
//			moveData(Group.class);
			moveData(RealTimeData.class);
			
		} catch (Exception e) {
			logger.error("获取取远大mongo数据出错");
			e.printStackTrace();
		}
	}
	
	public Object getValue(Object owner, String methodName) throws Exception {
		Class<?> ownerClass = owner.getClass();
		Method method = ownerClass.getMethod(methodName);
//		Class<?> returnType = method.getReturnType();
		return  method.invoke(owner);
	}  
	
	public <K> void moveData(Class<K> owner) throws Exception {
		List<K> findData=new ArrayList<>();
		if(mongoTemplate.collectionExists(owner)) {
			long secondCount = secondMongoTemplate.query(owner).count();
			long count = mongoTemplate.query(owner).count();
			logger.info("-----"+count+"--"+secondCount+"---"+owner.getName());
			if(secondCount>count) {
				Query query=new Query().with(new Sort(Direction.DESC, "createdAt")).limit(1);
				
				K findOne =  mongoTemplate.findOne(query, owner);
				if(findOne!=null) {
					Object value=getValue(findOne, "getCreatedAt");
					Query queryData=new Query(Criteria.where("createdAt").gt(value)).with(new Sort(Direction.ASC, "createdAt")).limit(1000);
					findData =  secondMongoTemplate.find(queryData, owner);
				}else {
//					findData= secondMongoTemplate.findAll(owner);
				}
			}
		}else {
			Query minQuery=new Query().with(new Sort(Direction.ASC, "createdAt")).limit(1);
			K data= secondMongoTemplate.findOne(minQuery, owner);
			mongoTemplate.insert(data);
		}
		
		if(!findData.isEmpty()) {
			mongoTemplate.insertAll(findData);
		}

	}
	*/
}
